const {
    getArticlename,
    createArticle,
    bandArticleAndLable,
    getarticlelist,
    getrecyclelist,
    getAllowTotop,
    toTop,
    getistotop,
    getArticleByid,
    editArticle,
    editArticlecorver,
    cancelTop,
    deleteArticleById,
    deleteArticleTag,
    searcharticle,
    getposandlist,
    addposition,
    getposition,
    getPreAndnextarticle,
    addview,
    getrecommentarticle,
    getarticlenums,
    getpos,
    setlike,
    searchinfo
} = require("../serve/article.js")
const Response = require("../util/respanse.js")
const sequelize = require("../model/db.js")
const articleAndLable = require("../model/articleAndLable.js")
/* 校验名字是否正确 */
const validataarticlename = async (req, res) => {
    try {
        let articleinfo = await getArticlename(req.query.name)
        if (articleinfo.code) {
            Response.successresponse(res, {
                code: 200,
                msg: "success",
                data: {
                    message: "ok"
                }
            })
        } else {
            Response.successresponse(res, {
                code: 400,
                msg: "文字名存在",
            })
        }
    } catch (error) {
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })
    }
}
/* 添加文章 */
const addarticle = async (req, res) => {
    const t = await sequelize.transaction();
    try {
        /* 解析参数 */
        const { lables, id, ...articleinfo } = req.body
        let article = await createArticle(articleinfo);
        let article_id
        if (article.code) {
            article_id = article.dataValues.id;
            /* 根据文章id绑定标签 */
            let isok = await bandArticleAndLable(article_id, lables);
            if (isok.code) {
                // 如果执行到此行,且没有引发任何错误提交事物
                await t.commit();
                Response.successresponse(res, {
                    code: 200,
                    message: "添加成功",
                    data: {
                        article_id
                    }
                })
            }
        } else {
            // 我们回滚事务.
            await t.rollback();
            Response.errorresponse(res, {
                code: 500,
                msg: "出错了",
                err: article.msg
            })
        }
    } catch (error) {
        console.log("3", error);
        await t.rollback();
        // 如果执行到达此行,则抛出错误.回滚事务.
        Response.errorresponse(res, {
            code: 500,
            msg: "出错了",
            err: error
        })

    }
}
/* 分页获取文章 */
const getarticle = async (req, res) => {
    console.log("dsadsadas");
    try {
        let { page, pageSize } = req.body
        let articlelist = await getarticlelist(page, pageSize);
        if (articlelist.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: {
                    articlelist: articlelist.data,
                    count: articlelist.count
                }
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: articlelist.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 400,
            msg: "获取失败",
            err: error
        })
    }
}
const getposlistarticle = async (req, res) => {
    try {
        let { page, pageSize } = req.body
        let articlelist = await getposandlist(page, pageSize);
        if (articlelist.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: {
                    articlelist: articlelist.data,
                    count: articlelist.count
                }
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: articlelist.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 400,
            msg: "获取失败",
            err: error
        })
    }
}
/* 分页获取草稿箱的文章数据 */
const getRecyclelist = async (req, res) => {
    try {
        let { page, pageSize } = req.body
        let articlelist = await getrecyclelist(page, pageSize);
        console.log(articlelist.msg);
        if (articlelist.code) {
            Response.successresponse(res, {
                code: 200,
                message: "获取成功",
                data: {
                    articlelist: articlelist.data,
                    count: articlelist.count
                }
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                data: {
                    count: articlelist.count,
                },
                msg: articlelist.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "获取失败",
            err: error
        })
    }
}
const getAllowTotops = async (req, res) => {
    let allowTotop = await getAllowTotop();
    if (allowTotop > 1) {
        Response.errorresponse(res, {
            code: 400,
            msg: "置顶文章数量超过2个"
        })
    } else {
        Response.successresponse(res, {
            code: 200,
            message: "获取成功"
        })
    }
}
/* 置顶文章 */
const toToparticle = async (req, res) => {
    try {
        let { article_id } = req.body
        /* 先查看文章是否置顶  置顶文章的数量大于三个就不让置顶小于三个就通过 */
        let istotop = await getistotop(article_id)
        if (istotop.code) {
            let allowTotop = await getAllowTotop();
            if (allowTotop > 1) {
                Response.errorresponse(res, {
                    code: 400,
                    msg: "置顶文章数量超过2个"
                })
            } else {
                /* 修改文章置顶 */
                let { article_id } = req.body;
                let result = await toTop(article_id);
                console.log(result.msg);
                if (result.code) {
                    Response.successresponse(res, {
                        code: 200,
                        message: "置顶成功"
                    })
                } else {
                    Response.errorresponse(res, {
                        code: 400,
                        msg: "置顶失败"
                    })
                }
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: istotop.msg
            })
        }
    } catch (error) {
        Response.errorresponse(res, {
            code: 500,
            msg: "置顶失败",
            err: error
        })
    }
}
/* 取消文章置顶 */
const cancelToparticle = async (req, res) => {
    try {
        let { article_id } = req.body;
        let istotop = await getistotop(article_id)
        if (!istotop.code) {
            /* 该文章已经置顶 取消置顶*/
            let result = await cancelTop(article_id);
            console.log(result.msg);
            if (result.code) {
                Response.successresponse(res, {
                    code: 200,
                    msg: "取消置顶成功"
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: result.msg
                })
            }
        }
    } catch (error) {
        Response.errorresponse(res, {
            code: 500,
            msg: "取消置顶失败",
            err: error
        })
    }
}
/* 根据文章id查询文章 */
const getarticlebyid = async (req, res) => {
    try {
        let { article_id } = req.query;
        let result = await getArticleByid(article_id);

        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: result.msg,
                data: result.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                message: "查询失败"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "查询失败",
            err: error
        })
    }
}
/* 编辑文章 */
const edmitarticle = async (req, res) => {
    try {
        /* 校验文章是否存在 */
        let { article_id, data } = req.body
        let { id, ...articleinfo } = data
        let ifexit = await getArticleByid(article_id)
        if (ifexit.code) {
            /* 文章存在 提交文章修改*/
            let result = await editArticle(article_id, articleinfo)
            if (result.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "修改成功"
                })
            } else {
                Response.errorresponse(res, {
                    code: 200,
                    msg: "修改失败"
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "编辑失败",
            err: error
        })
    }
}
/* 上传文件名字 */
const editfileName = async (req, res) => {
    try {
        /* 先查看文章是否存在 */
        let { article_id, file_name } = req.body;
        let result = await getArticleByid(article_id);
        if (result.code) {
            /* 文章存在时候，修改文章的corver */
            let ress = await editArticlecorver(article_id, file_name);
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: "修改成功"
                })
            } else {
                Response.errorresponse(res, {
                    code: 200,
                    msg: "修改失败"
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章不存在"
            })
        }
    } catch (error) {

    }
}
/* 删除文章 */
const deleteArticle = async (req, res) => {
    try {
        const t = await sequelize.transaction();
        /* 先查看文章是否存在 */
        let { article_id } = req.body;
        let result = await getArticleByid(article_id);
        if (result.code) {
            let ress = await deleteArticleById(article_id);
            if (ress.code) {
                /* 删除对应的标签 */
                let resss = await deleteArticleTag(article_id);
                if (resss.code) {
                    await t.commit()
                    Response.successresponse(res, {
                        code: 200,
                        message: "删除成功"
                    })

                } else {
                    await t.rollback();
                    Response.successresponse(res, {
                        code: 200,
                        message: "删除成功"
                    })
                }
            } else {
                await t.rollback();
                Response.errorresponse(res, {
                    code: 400,
                    msg: "删除失败"
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "删除失败",
            err: error
        })
    }
}
/* 搜索文章 */
const getarticlebycondition = async (req, res) => {
    let { article_title, article_istop, article_status, lable_id, page, pageSize, time } = req.body
    try {
        let ress = await searcharticle({ article_title, article_istop, article_status, lable_id, page, pageSize, time })
        console.log(ress.msg);
        if (ress.code) {
            Response.successresponse(res, {
                code: 200,
                message: ress.msg,
                data: ress.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: ress.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "搜索失败",
            err: error
        })
    }
}
/* 提交地点 */
const subposition = async (req, res) => {
    try {
        let { pos, article_id } = req.body
        let rees = await getposition(article_id)
        if (rees.code) {
            let ress = await addposition(pos, article_id)
            if (ress.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: ress.msg,
                    data: ress.data
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: ress.msg
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章已经绑定足迹"
            })
        }

    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "搜索失败",
            err: error
        })
    }
}
/* 获取文章上一篇下一篇 */
const getPreAndnext = async (req, res) => {
    let { article_id } = req.query
    if (!article_id) {
        Response.errorresponse(res, {
            code: 400,
            msg: "文章id不能为空"
        })
    } else {
        let re = await getPreAndnextarticle(article_id)
        if (re.code) {
            Response.successresponse(res, {
                code: 200,
                message: re.msg,
                data: re.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: re.msg
            })
        }
    }

}
/* 添加访问量 */
const addarticleview = async (req, res) => {
    let { article_id } = req.query
    if (!article_id) {
        Response.errorresponse(res, {
            code: 400,
            msg: "文章id不能为空"
        })
    } else {
        let re = await addview(article_id)
        if (re.code) {
            Response.successresponse(res, {
                code: 200,
                message: re.msg,
                data: re.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: re.msg
            })
        }
    }

}
/* 获取推荐文章 */
const getrecommentdarticle = async (req, res) => {
    let re = await getrecommentarticle()
    if (re.code) {
        Response.successresponse(res, {
            code: 200,
            message: re.msg,
            data: re.data
        })
    } else {
        Response.errorresponse(res, {
            code: 400,
            msg: re.msg
        })
    }
}
/* 获取文章数量 */
const getarticlenum = async (req, res) => {
    let re = await getarticlenums()
    if (re.code) {
        Response.successresponse(res, {
            code: 200,
            message: re.msg,
            data: re.data
        })
    } else {
        Response.errorresponse(res, {
            code: 400,
            msg: re.msg
        })
    }
}
/* 获取地址和文章 */
const getarticlebypos = async (req, res) => {
    try {
        let re = await getpos()
        if (re.code) {
            Response.successresponse(res, {
                code: 200,
                message: re.msg,
                data: re.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: re.msg
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "搜索失败",
            err: error
        })
    }
}
/* 带赞 */
const addlike = async (req, res) => {
    try {
        let { article_id, status } = req.body
        let result = await getArticleByid(article_id);
        if (result.code) {
            let re = await setlike(article_id, status)
            if (re.code) {
                Response.successresponse(res, {
                    code: 200,
                    message: re.msg,
                })
            } else {
                Response.errorresponse(res, {
                    code: 400,
                    msg: re.msg
                })
            }
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章不存在"
            })
        }

    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "失败",
            err: error
        })
    }
}
/* 搜说  */
const serchinfo = async (req, res) => {
    try {
        let { keyword } = req.body
        let result = await searchinfo(keyword)
        if (result.code) {
            Response.successresponse(res, {
                code: 200,
                message: result.msg,
                data:result.data
            })
        } else {
            Response.errorresponse(res, {
                code: 400,
                msg: "文章不存在"
            })
        }
    } catch (error) {
        console.log(error);
        Response.errorresponse(res, {
            code: 500,
            msg: "失败",
            err: error
        })
    }
}
module.exports = {
    validataarticlename,
    getarticle,
    addarticle,
    getRecyclelist,
    toToparticle,
    getarticlebyid,
    edmitarticle,
    editfileName,
    cancelToparticle,
    deleteArticle,
    getarticlebycondition,
    getposlistarticle,
    subposition,
    getAllowTotops,
    getPreAndnext,
    addarticleview,
    getrecommentdarticle,
    getarticlenum,
    getarticlebypos,
    addlike,
    serchinfo
}